package com.wuxiaofan.product.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.product.entity.Brand;
import com.wuxiaofan.product.entity.Category;
import com.wuxiaofan.product.entity.CategoryBrandRelation;
import com.wuxiaofan.product.entity.custom.CategoryBrandRelationCustom;
import com.wuxiaofan.product.mapper.BrandMapper;
import com.wuxiaofan.product.mapper.CategoryBrandRelationMapper;
import com.wuxiaofan.product.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wuxiaofan.product.client.Idgenerator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

@Service
public class CategoryBrandRelationService {
    @Autowired
    private CategoryBrandRelationMapper pmsCategoryBrandRelationMapper;
    @Resource
    private BrandMapper brandMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private Idgenerator idgenerator;
    public CategoryBrandRelation findById(String id){
        return pmsCategoryBrandRelationMapper.selectById(id);
    }

    public List<CategoryBrandRelationCustom> findByBrandId(String brandId){
        Map paraMap = new HashMap();
        paraMap.put("brandId",brandId);
        return findByMap(paraMap);
    }

    public CategoryBrandRelationCustom findCountByMap(Map paraMap){
        return pmsCategoryBrandRelationMapper.findCountByMap(paraMap);
    }

    public CategoryBrandRelationCustom findGroupByMap(Map paraMap){
        return pmsCategoryBrandRelationMapper.findGroupByMap(paraMap);
    }

    public List<CategoryBrandRelationCustom> findByMap(Map paraMap){
        return pmsCategoryBrandRelationMapper.findByMap(paraMap);
    }

    public Page<CategoryBrandRelationCustom> findByMap(Map paraMap, Page page){
        return page.setRecords(pmsCategoryBrandRelationMapper.findByMap(paraMap,page));
    }


    public void updateMapById(Map paraMap){
        pmsCategoryBrandRelationMapper.updateMapById(paraMap);
    }
    public void delById(String id){
        pmsCategoryBrandRelationMapper.deleteById(id);
    }

    public CategoryBrandRelation save(CategoryBrandRelation pmsCategoryBrandRelation){
        pmsCategoryBrandRelation.setId(idgenerator.getId());
        Category category = categoryMapper.selectById(pmsCategoryBrandRelation.getCatelogId());
        Brand brand = brandMapper.selectById(pmsCategoryBrandRelation.getBrandId());
        pmsCategoryBrandRelation.setCatelogName(category.getName());
        pmsCategoryBrandRelation.setBrandName(brand.getName());
        pmsCategoryBrandRelationMapper.insert(pmsCategoryBrandRelation);
        return pmsCategoryBrandRelation;
    }

}
